package com.yixia.camera.demo.extend;

import java.util.ArrayList;
import java.util.LinkedList;

import com.yixia.weibo.sdk.MediaRecorderBase;
import com.yixia.weibo.sdk.MediaRecorderBase$OnEncodeListener;
import com.yixia.weibo.sdk.MediaRecorderBase$OnErrorListener;
import com.yixia.weibo.sdk.MediaRecorderNative;
import com.yixia.weibo.sdk.VideoProcessEngine;
import com.yixia.weibo.sdk.VideoProcessEngine$OnLocalMusicLoadListener;
import com.yixia.weibo.sdk.VideoProcessEngine$OnMusicApplyListener;
import com.yixia.weibo.sdk.VideoProcessEngine$OnVideoEncodingListener;
import com.yixia.weibo.sdk.VideoProcessEngine$OnVideoPlayListener;
import com.yixia.weibo.sdk.model.MediaObject;
import com.yixia.weibo.sdk.model.MediaObject$MediaPart;
import com.yixia.weibo.sdk.model.VideoEffectModel;
import com.yixia.weibo.sdk.model.VideoMusicModel;

public class MediaRecorderTool {
	public static boolean startRecord(MediaRecorderBase mMediaRecorder) {
		// TODO Auto-generated method stub
		MediaObject$MediaPart part = mMediaRecorder.startRecord();
		if (part == null) {
			return false;
		}
		return true;
	}

	public static String startEncoding(MediaObject mMediaObject, int duration) {
		// TODO Auto-generated method stub
		// 生成片段信息
		com.yixia.weibo.sdk.model.MediaObject$MediaPart part = mMediaObject
				.getLastPart();
		if (part == null) {
			part = mMediaObject.buildMediaPart(-1, ".mp4");
		}
		part.duration = duration;
		String output = part.mediaPath;
		return output;
	}

	/** 预处理数据对象 */
	public static void preparedMediaObject(MediaObject mMediaObject) {
		if (mMediaObject != null && mMediaObject.getMedaParts() != null) {
			int duration = 0;
			for (com.yixia.weibo.sdk.model.MediaObject$MediaPart part : (LinkedList<com.yixia.weibo.sdk.model.MediaObject$MediaPart>) mMediaObject
					.getMedaParts()) {
				part.startTime = duration;
				part.endTime = part.startTime + part.duration;
				duration += part.duration;
			}
		}
	}

	public static boolean getCurrentPart(MediaObject mMediaObject) {
		MediaObject$MediaPart part = mMediaObject.getCurrentPart();
		if (part != null) {
			if (part.remove) {
				part.remove = false;
				return true;
			} else {
				part.remove = true;
			}
		}
		return false;
	}

	/** 取消回删 */
	public static boolean cancelDelete(MediaObject mMediaObject) {
		if (mMediaObject != null) {
			MediaObject$MediaPart part = mMediaObject.getCurrentPart();
			if (part != null && part.remove) {
				part.remove = false;
				return true;
			}
		}
		return false;
	}

	/** 回删 */
	public static boolean backRemove(MediaObject mMediaObject) {
		if (mMediaObject != null && mMediaObject.mediaList != null) {
			int size = mMediaObject.mediaList.size();
			if (size > 0) {
				com.yixia.weibo.sdk.model.MediaObject$MediaPart part = (MediaObject$MediaPart) mMediaObject.mediaList
						.get(size - 1);
				mMediaObject.removePart(part, true);

				if (mMediaObject.mediaList.size() > 0)
					mMediaObject.mCurrentPart = (MediaObject$MediaPart) mMediaObject.mediaList
							.get(mMediaObject.mediaList.size() - 1);
				else
					mMediaObject.mCurrentPart = null;
				return true;
			}
		}
		return false;
	}

	public static void applyMusic(VideoProcessEngine videoProcessEngine,
			VideoMusicModel theme, final OnVideoPlayListener onVideoPlayListener) {
		videoProcessEngine.applyMusic(theme,
				new VideoProcessEngine$OnVideoPlayListener() {

					@Override
					public void onError(int errorCode) {
						// TODO Auto-generated method stub
						onVideoPlayListener.onError(errorCode);
					}

					@Override
					public void onBufferStart() {
						// TODO Auto-generated method stub
						onVideoPlayListener.onBufferStart();
					}

					@Override
					public void onBufferEnd() {
						// TODO Auto-generated method stub
						onVideoPlayListener.onBufferEnd();
					}

					@Override
					public void onCompletion() {
						// TODO Auto-generated method stub
						onVideoPlayListener.onCompletion();
					}
				});
	}

	public static void applyMusic(VideoProcessEngine videoProcessEngine,
			VideoMusicModel theme,
			final OnVideoPlayListener onVideoPlayListener,
			final OnMusicApplyListener onMusicApplyListener) {
		videoProcessEngine.applyMusic(theme,
				new VideoProcessEngine$OnVideoPlayListener() {

					@Override
					public void onError(int errorCode) {
						// TODO Auto-generated method stub
						onVideoPlayListener.onError(errorCode);
					}

					@Override
					public void onBufferStart() {
						// TODO Auto-generated method stub
						onVideoPlayListener.onBufferStart();
					}

					@Override
					public void onBufferEnd() {
						// TODO Auto-generated method stub
						onVideoPlayListener.onBufferEnd();
					}

					@Override
					public void onCompletion() {
						// TODO Auto-generated method stub
						onVideoPlayListener.onCompletion();
					}
				}, new VideoProcessEngine$OnMusicApplyListener() {

					@Override
					public void downloadAssetsProcess(int arg0) {
						// TODO Auto-generated method stub
						onMusicApplyListener.downloadAssetsProcess(arg0);
					}

					@Override
					public void downloadAssetsSuccess(VideoEffectModel arg0) {
						// TODO Auto-generated method stub
						onMusicApplyListener.downloadAssetsSuccess(arg0);
					}

					@Override
					public void onFailed(int arg0) {
						// TODO Auto-generated method stub
						onMusicApplyListener.onFailed(arg0);
					}

					@Override
					public void onSuccess(String arg0) {
						// TODO Auto-generated method stub
						onMusicApplyListener.onSuccess(arg0);
					}

				});
	}

	public static void applyEffect(VideoProcessEngine videoProcessEngine,
			int var1, int var2, final OnVideoPlayListener onVideoPlayListener) {
		// TODO Auto-generated method stub
		videoProcessEngine.applyEffect(var1, var2,
				new VideoProcessEngine$OnVideoPlayListener() {

					@Override
					public void onError(int errorCode) {
						// TODO Auto-generated method stub
						onVideoPlayListener.onError(errorCode);
					}

					@Override
					public void onBufferStart() {
						// TODO Auto-generated method stub
						onVideoPlayListener.onBufferStart();
					}

					@Override
					public void onBufferEnd() {
						// TODO Auto-generated method stub
						onVideoPlayListener.onBufferEnd();
					}

					@Override
					public void onCompletion() {
						// TODO Auto-generated method stub
						onVideoPlayListener.onCompletion();
					}
				});
	}

	public static void saveVideoToPath(VideoProcessEngine videoProcessEngine,
			String mVideoPath,
			final OnVideoEncodingListener onVideoEncodingListener) {
		// TODO Auto-generated method stub
		videoProcessEngine.saveVideoToPath(mVideoPath,
				new VideoProcessEngine$OnVideoEncodingListener() {

					@Override
					public void onFailed(int arg0) {
						// TODO Auto-generated method stub
						onVideoEncodingListener.onFailed(arg0);
					}

					@Override
					public void onProgressChanged(int arg0) {
						// TODO Auto-generated method stub
						onVideoEncodingListener.onProgressChanged(arg0);
					}

					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						onVideoEncodingListener.onSuccess();
					}

				});
	}
	
	public static void loadLocalMusic(VideoProcessEngine videoProcessEngine,String var1,final OnLocalMusicLoadListener onLocalMusicLoadListener){
		videoProcessEngine.loadLocalMusic(var1,new VideoProcessEngine$OnLocalMusicLoadListener(){

			@Override
			public void onFailed() {
				// TODO Auto-generated method stub
				onLocalMusicLoadListener.onFailed();
			}

			@Override
			public void onSuccess(ArrayList arg0) {
				// TODO Auto-generated method stub
				onLocalMusicLoadListener.onSuccess(arg0);
			}});
	}
	
	public static void initMediaRecorder(MediaRecorderBase mMediaRecorder,final OnErrorListener onErrorListener,final OnEncodeListener onEncodeListener){
		mMediaRecorder.setOnErrorListener(new MediaRecorderBase$OnErrorListener(){

			@Override
			public void onAudioError(int arg0, String arg1) {
				// TODO Auto-generated method stub
				onErrorListener.onAudioError(arg0, arg1);
			}

			@Override
			public void onVideoError(int arg0, int arg1) {
				// TODO Auto-generated method stub
				onErrorListener.onVideoError(arg0, arg1);
			}
			
		});
		mMediaRecorder.setOnEncodeListener(new MediaRecorderBase$OnEncodeListener(){

			@Override
			public void onEncodeComplete() {
				// TODO Auto-generated method stub
				onEncodeListener.onEncodeComplete();
			}

			@Override
			public void onEncodeError() {
				// TODO Auto-generated method stub
				onEncodeListener.onEncodeError();
			}

			@Override
			public void onEncodeProgress(int arg0) {
				// TODO Auto-generated method stub
				onEncodeListener.onEncodeProgress(arg0);
			}

			@Override
			public void onEncodeStart() {
				// TODO Auto-generated method stub
				onEncodeListener.onEncodeStart();
			}
			
		});
	}
}
